﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data.OracleClient;

namespace XMLToWordLib
{
    public class DBLinkUser
    {
        string name;
        string id;
        bool StartMark = false;
        string connectionString;
        OracleConnection connection ;        
        OracleCommand command ;




        public string Name
        {
            get { return name; }
            set { name = value; }
        }        

        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        public string ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; }
        }


        public OracleConnection Connection
        {
            get { return connection ; }
        }


        public OracleCommand OraCommand
        {
            get { return command; }
        }

        public void Start()
        {
            if (StartMark) return;
            if (connectionString == null)
            {
                throw new Exception("请先设置连接字符串");
            }
            if (connection == null)
            {
                connection = new OracleConnection(connectionString);
                connection.Open();
                command = connection.CreateCommand(); 
            }
            StartMark = true;     
        }


        public int GetTJNum(string sql)
        {
            int rtnNum = 0;
            command.CommandText = sql;
            OracleDataReader reader=null;
            try{
                reader = command.ExecuteReader();
                reader.Read();
                rtnNum=int.Parse( reader[0].ToString());
            }
            finally
            {
                if(reader!=null) reader.Close();
            }
            return rtnNum;
        }



        public void Close()
        {
            //if (command !=null) command.

            if (command != null)
            {
                command.Dispose();
            }

            if (connection != null)
            {

                try
                {
                    connection.Close();
                    connection = null;
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }


        ~DBLinkUser()
        {
            Close();
        
        }

    }
}
